
const app = getApp()
Page({
	data: {
		userInfo: {},
		canIUseGetUserProfile: false,
		changeShow: false,
		couponCode: '',
		systemOs: app.globalData.systemOs
	},
	onLoad: function (options) {
		console.log(app.globalData.systemOs)
		var _this = this
		if (wx.getUserProfile) {
			console.log('11')
			this.setData({
				canIUseGetUserProfile: true
			})
		}
		if (app.userinfo) {
			if (app.userinfo.nickName != '微信用户') {
				_this.setData({
					userInfomin: app.userinfo,
					hasUserInfo: true
				})
			} else {
				_this.setData({
					userInfomin: '',
					hasUserInfo: false
				})
			}
		} else {
			_this.setData({
				userInfomin: '',
				hasUserInfo: false
			})
		}
	},
	onShow(){
		this.getUserMessage()
	},
	toPop(e) {
		this.setData({
			couponCode: '',
			changeShow: true
		})
	},
	onClose() {
		this.setData({
			changeShow: false
		})
	},
	// 优惠券兑换
	onConfirm() {
		let _this = this;
		if (_this.couponCode == '') {
			wx.showToast({
				title: '请输入兑换码',
				icon: 'error',
				duration: 1500
			})
			return
		}
		app.util.request({
			url: "api/exchangeDiscount",
			method: "post",
			data: {
				discountCode: _this.data.couponCode
			},
			success(response) {
				if (response.data.code == 0) {
					wx.showToast({
						title: '兑换成功！',
						icon: 'success',
						duration: 1500
					})
				} else {
					wx.showToast({
						title: '兑换失败！',
						icon: 'none'
					})
				}
			}
	})
	},
    getUserProfile() {
			let _this = this;
        if (wx.getUserProfile) {
            wx.getUserProfile({
                desc: '用于注册用户信息',
                lang: 'zh_CN',
                success: (res) => {
                    app.userinfo = res.userInfo
                    wx.setStorageSync('userinfo', res.userInfo)
										console.log(res)
										console.log(wx.getStorageSync('userinfo'))
                    this.setData({
                        userInfomin: res.userInfo,
                        hasUserInfo: true,
                    });
                    app.util.request({
                        url: "app/message",
                        data: {
                            avatar: res.userInfo.avatarUrl,
                            nickName: res.userInfo.nickName,
                            sex:res.userInfo.gender
                        },
                        success(response) {
                            if (response.data.code == 0) {}
                        }
                    })
                },
                fail(err) {
                    console.log(err)
                }
            })
        }
    },
    getUserInfo(e) {
        if (e.detail.errMsg == "getUserInfo:ok") {
            let userinfo = e.detail.userInfo
            wx.setStorageSync('userinfo', userinfo)
            this.setData({
                userInfomin: userinfo,
                hasUserInfo: true,
            });
            app.util.request({
                url: "app/message",
                data: {
                    avatar: userinfo.avatarUrl,
                    nickname: userinfo.nickName,
										sex: userinfo.gender
                },
                success(response) {
                    if (response.data.code == 0) {}
                }
            })
        }
    },
	toTrading() {
		wx.navigateTo({
			url: '/pages/goods/my-trading/my-trading?remark=' + this.data.userInfo.remark + "&active=0"
		})
	},
	toMyGoods() {
		wx.navigateTo({
			url: '/pages/goods/my-goods-index'
		})
	},
	getUserMessage() {
		let _this = this;
		app.util.request({
			url: "api/account/selectByOpen_id",
			method: "post",
			success(response) {
				if (response.data.code == 0) {
					app.globalData.userInfo = response.data.data
					_this.setData({
						userInfo: response.data.data,
					})
				}
			}
		})
	},
	toMember() {
		wx.navigateTo({
			url: '/pages/personal/open-member/open-member',
		})
	},
	toOrder(e) {
		wx.navigateTo({
			url: '/pages/order/order-index?state='+e.currentTarget.dataset.state,
		})
	}
})
